package com.gopay.scheduler.job;

import com.gopay.common.domain.acps.AcctBalanceWarning;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.balance.BalanceWarningService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @classname:SendBalanceWarningNotifyJob
 * @description:余额预警自动发送通知邮件任务
 * @date:2017/12/18/018
 * @author :PANSHICHAO
 * www.gopay.com.cn Inc.All rights reserved.
 */
public class SendBalanceWarningNotifyJob extends GopayBaseJob {
    @Resource(name="balanceWarningService")
    private BalanceWarningService balanceWarningService;
    private static final Logger LOGGER = LoggerFactory.getLogger(SendBalanceWarningNotifyJob.class);
    @Override
    protected void doJobInternal() {
        LOGGER.error("余额预警通知任务开始执行");
        List<Object[]> resultList =balanceWarningService.showAllCustCorpInfoByIsBalanceWarningConfig();
        if(resultList == null){
            LOGGER.error("无开通余额预警的商户，无需执行任务");
        }else{
            for(Object[] objItem : resultList){
                String acctId = (String)objItem[1];
                LOGGER.error("开通余额预警信息的账号为："+acctId);
                Date[] resultDate = balanceWarningService.getStartDateAndEndDate();
                AcctBalanceWarning balanceWarnings = balanceWarningService.showAllAcctBalanceWarningInfoByAcctId(acctId);
                BigDecimal quota = balanceWarnings.getBalanceNotifyQuota();
                LOGGER.error("当前通知余额为："+quota);
                if(quota.compareTo(new BigDecimal("0.00")) == 0){
                    sendEmailOrPhoneOrAllBalanceType(acctId,resultDate[0],resultDate[1],balanceWarnings);
                }else{
                    Date date = new Date();
                    long now = date.getTime();
                    long target=balanceWarnings.getLastUpdTs().getTime();
                    LOGGER.error("上一次发送时间毫米值："+target+",当前时间对应的毫秒值为："+now);
                    if((now - target)/1000/60 >30||(now - target)/1000/60 ==30){
                        sendEmailOrPhoneOrAllBalanceType(acctId,resultDate[0],resultDate[1],balanceWarnings);
                    }else{
                        LOGGER.error("当前时间间隔未满三十分钟不发送余额预警通知！");
                    }
                }
            }
        }
        LOGGER.error("余额预警通知任务执行结束");
    }

    private void sendEmailOrPhoneOrAllBalanceType(String acctId,Date startDate,Date endDate, AcctBalanceWarning balanceWarnings){
        int resultCount = balanceWarningService.showInfoCountByCustIdAndAcctIdAndBewteenDate(acctId,startDate,endDate);
        LOGGER.error("开通余额预警通知的账号的交易信息数为："+resultCount);
        BigDecimal result = balanceWarningService.showNowsAcctQuotaInfoByAcctIdAndBetweenDate(acctId,startDate,endDate);
        LOGGER.error("开通余额预警通知的账号的当前时间的余额为："+result);
        if(resultCount!=0 ){
            LOGGER.error("开通余额预警账户的预计余额为："+balanceWarnings.getBalanceWarningQuota());
            if(balanceWarnings.getBalanceNotifyQuota() == null){
                balanceWarnings.setBalanceNotifyQuota(new BigDecimal("0.00"));
            }
            LOGGER.error("开通余额预警账户的预计余额为："+balanceWarnings.getBalanceWarningQuota());
            LOGGER.error("上一次通知的余额为："+balanceWarnings.getBalanceNotifyQuota());
            if(result.compareTo(balanceWarnings.getBalanceWarningQuota())<0 && result.compareTo(new BigDecimal("0.00")) >0 && result.compareTo(balanceWarnings.getBalanceNotifyQuota())!=0){
                LOGGER.error("余额小于预警余额，开始发送邮件");
                balanceWarningService.sendBalanceWarningToEmailOrPhone(balanceWarnings);
                balanceWarnings.setBalanceNotifyQuota(result);
                balanceWarnings.setLastUpdTs(new Date());
                balanceWarningService.updateBalanceNotifyQuota(balanceWarnings);
            }else{
                LOGGER.error("余额未小于预警余额，无邮件预警通知！");
            }
        }else{
            LOGGER.error(acctId+"今日余额无变动不发送余额预警信息");
        }
    }
}
